package activity.personal;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.hutoubenjg.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import adapter.NoScrollGridAdapter;
import base.BaseActivity;
import base.MyApplcation;
import bean.measge_bean.WeiXiuiJiLuBean;
import photu.ImagePagerActivity;
import presenter.SysTemPresenter;
import util.NoScrollGridView;
import util.utls.Const;
import util.utls.IsUtilUid;
import util.utls.XImageLoader;
import util.volley.ResultEntity;

/**
 * Created by itboye on 2017/1/3.
 */

public class ActivityWeiXiuXiangqing extends BaseActivity implements Observer {

    TextView tvTitle, tvweixiu01, tvweixiu02, tvweixiu03, tvweixiu04;
    String WeiXiu1, WeiXiu2, WeiXiu3, WeiXiu4;
    String info;
    NoScrollGridView gridview;
    ImageView back;
    WeiXiuiJiLuBean bean;
    SysTemPresenter presenter;
    int pageNum = 1;
    int pageSize = 10;
    LinearLayout img0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weixiuxiangqing);
        tvTitle.setText("维修详情");
        WeiXiu1 = getIntent().getStringExtra("WeiXiu1");
        WeiXiu2 = getIntent().getStringExtra("WeiXiu2");
        WeiXiu3 = getIntent().getStringExtra("WeiXiu3");
        WeiXiu4 = getIntent().getStringExtra("WeiXiu4");
        info = getIntent().getStringExtra("info");
        tvweixiu01.setText("¥:" +Double.parseDouble(WeiXiu1)/100 + "");
        tvweixiu02.setText("¥:" +Double.parseDouble(WeiXiu2)/100 + "");
        tvweixiu03.setText("¥:" + Double.parseDouble(WeiXiu3)/100 + "");
        tvweixiu04.setText(WeiXiu4 + "");



        String str = info;// 原始字符串

        String[] arrayStr = new String[]{};// 字符数组
        List<String> lists = new ArrayList<String>();// list

        arrayStr = str.split(",");// 字符串转字符数组
        lists = java.util.Arrays.asList(arrayStr);// 字符数组转list
        final String[] storeStr = lists.toArray(new String[lists.size()]);// list转成字符数组
         img0.removeAllViews();
        /* 输出字符数组的值 */
        for (final String s : arrayStr) {
            System.out.println(s);

            if (storeStr.length <= 0) {

                img0.setVisibility(View.GONE);
            } else   if (s == null || s.equals("")) {
                img0.setVisibility(View.GONE);
            }else if (storeStr.length >= 1) {
                 img0.setVisibility(View.VISIBLE);
                View v = LayoutInflater.from(MyApplcation.ctx).inflate(R.layout.layout_items, null);
                ImageView img1 = (ImageView) v.findViewById(R.id.img1);
                XImageLoader.load(Const.IMAGE_HEAD + s, img1);
                ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(
                        ViewGroup.MarginLayoutParams.WRAP_CONTENT,
                        ViewGroup.MarginLayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(5, 5, 5, 5);

                img1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent intent=new Intent(ActivityWeiXiuXiangqing.this,ImageVieMaxActivyt.class);
                        intent.putExtra("id",s);
                        startActivity(intent);
                    }
                });
                  img0.addView(v, layoutParams);
            }

        }


        presenter = new SysTemPresenter(this);
        bean = new WeiXiuiJiLuBean();
        presenter.onWeiXiuJiLu(IsUtilUid.isUid(), IsUtilUid.isReparl(),"8" ,pageNum + "", pageSize + "");

//        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                // TODO Auto-generated method stub
//                imageBrower(position, infoList);
//            }
//        });
    }
    /**
     * 打开图片查看器
     *
     * @param position
     * @param urls2
     */
    protected void imageBrower(int position, ArrayList<String> urls2) {
        Intent intent = new Intent(mContext, ImagePagerActivity.class);
        // 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls2);
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
        mContext.startActivity(intent);
    }
    /**
     * 打开图片查看器
     * <p>
     * //     * @param position
     * //     * @param urls2
     */
//    protected void imageBrower(int position, ArrayList<String> urls2) {
//        Intent intent = new Intent(mContext, ImagePagerActivity.class);
//        // 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
//        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls2);
//        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
//        startActivity(intent);
//    }
    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == presenter.WeiXiuJilu_success) {
                bean = (WeiXiuiJiLuBean) entity.getData();
                gridview.setAdapter(new NoScrollGridAdapter(MyApplcation.ctx, bean));

            }
        }
    }
}
